﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using STEditor.Classes;
using System.IO;
using System.ComponentModel;
using Microsoft.Win32;

namespace StEditor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private STGame _game;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            var gameArea = new STGameArea { TileWidth = 32, TileHeight = 32, Width = 12, Height = 8, AreaName="Area1" };
            _game = new STGame();
            _game.GameAreas.Add(gameArea);

            DataContext = _game;

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var image = ((FrameworkElement)sender).DataContext as STFrame;
            _game.TileTypes.SelectedTile = image;
        }

        private void Areas_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            var selectedArea = ((ComboBox)sender).SelectedItem as STGameArea;
            _game.CurrentArea = selectedArea;

        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {

            var saveDialog = new System.Windows.Forms.FolderBrowserDialog();

            if (saveDialog.ShowDialog(new WinFormsHandle(this)) == System.Windows.Forms.DialogResult.OK)
            {
                string folderName = saveDialog.SelectedPath;
                new STGameXamlWriter().Save(_game, folderName);
            }
        }


        private void Load_Click(object sender, RoutedEventArgs e)
        {

            var loadDialog = new System.Windows.Forms.FolderBrowserDialog();

            if (loadDialog.ShowDialog(new WinFormsHandle(this)) == System.Windows.Forms.DialogResult.OK)
            {
                string folderName = loadDialog.SelectedPath;

                var game = new STGameXamlLoader().Load(folderName);
                _game = game;
                DataContext = game;
                game.CurrentArea = game.GameAreas.FirstOrDefault();
            }

        }

        private void AddTile_Click(object sender, RoutedEventArgs e)
        {

            var openFile = new OpenFileDialog
            {
                Multiselect = true,
                Filter = "PNG files|*.png|All files|*.*"
            };
            if (openFile.ShowDialog(this).Value)
            {

                openFile
                    .FileNames
                    .ToList()
                    .ForEach(t => _game.TileTypes.Add(t));
                
            }

        }

        private void FillMode_Click(object sender, RoutedEventArgs e)
        {
           // _game.FillMode = FillMode.IsChecked.Value;
        }


    }
}
